package com.houg.testuserapi.controller;

import com.houg.testuserapi.common.constants.Base;
import com.houg.testuserapi.common.resp.Resp;
import com.houg.testuserapi.model.entity.User;
import com.houg.testuserapi.model.vo.OrderVo;
import com.houg.testuserapi.service.GetOrderService;
import com.houg.testuserapi.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @Author: NALHOUG
 * @Time: 2019/11/8 15:33
 * @Explain: 用户控制层
 */
@Slf4j
@RestController
public class GetOrderController {
    @Resource
    private UserService userService;
    @Resource
    private GetOrderService getOrderService;

    /**
     * 查看订单
     * @return resp 返回对象
     */

    @ResponseBody
    @RequestMapping(value = "/order", method = RequestMethod.POST)
    public Resp<OrderVo> selectOrder(HttpServletRequest request){
        Resp<OrderVo> resp = new Resp<OrderVo>();
        String id = thisAccount(request).getUserId();
        log.info(id);
        resp.setData(getOrderService.selectUserOrder(id));
        return resp;
    }

    /**
     * 付款
     * @return resp 返回对象
     */
    @ResponseBody
    @RequestMapping(value = "/pay", method = RequestMethod.POST)
    public Resp<String> getPay(HttpServletRequest request){
        Resp<String> resp = new Resp<String>();
        OrderVo orderVo = getOrderService.selectUserOrder(thisAccount(request).getUserId());
        resp.setMsg(getOrderService.getPay(orderVo.getOrderId()));
        return resp;
    }

    /**
     * 获取Session中的用户
     * @param request SessionID
     * @return request Session用户
     */
    private User thisAccount(HttpServletRequest request){
        return (User)request.getSession().getAttribute(Base.THIS_USER);
    }

}
